//@Time:2022/4/4 12:23
//@Author:aFun

package indi.csms_springboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import indi.csms_springboot.domain.User;
import indi.csms_springboot.service.ManagerService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@RestController
@RequestMapping("/managerController")
public class ManagerController {

    @Autowired
    private ManagerService managerService;

    @GetMapping
    public ModelAndView findUserByPage(HttpServletRequest request,ModelAndView modelAndView){

        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");

        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)) {
            rows = "5";
        }
        Map<String, String[]> condition = request.getParameterMap();
        IPage<User> pb = managerService.findUserByPage(currentPage, rows, condition);
        request.setAttribute("pb", pb);
        request.setAttribute("condition", condition);
        modelAndView.setViewName("managerList");
        return modelAndView;
    }

    @PostMapping
    public ModelAndView addUser(HttpServletRequest request) throws UnsupportedEncodingException {
        ModelAndView modelAndView=new ModelAndView();
        request.setCharacterEncoding("utf-8");
        Map<String, String[]> map = request.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user, map);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        boolean b = managerService.addUser(user);
        modelAndView.addObject("add_flag",b);
        modelAndView.setViewName("redirect:"+request.getContextPath()+"/managerController");
        return modelAndView;
    }

    @RequestMapping("/updateUser")
    public ModelAndView updateUser(HttpServletRequest request) throws UnsupportedEncodingException {
        ModelAndView modelAndView=new ModelAndView();
        request.setCharacterEncoding("utf-8");
        Map<String, String[]> map = request.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user, map);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        boolean b = managerService.updateUser(user);
        modelAndView.setViewName("redirect:"+request.getContextPath()+"/managerController");
        return modelAndView;
    }


    @RequestMapping("/deleteUser")
    public ModelAndView deleteUser(HttpServletRequest request) throws UnsupportedEncodingException {
        String uid = request.getParameter("uid");
        ModelAndView modelAndView=new ModelAndView();
        boolean b = managerService.deleteUserById(Integer.parseInt(uid));
        modelAndView.setViewName("redirect:"+request.getContextPath()+"/managerController");
        return modelAndView;
    }


    @RequestMapping("/deleterUsers")
    public ModelAndView deleteUsers(HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView();
        String[] ids = request.getParameterValues("uid");
        boolean b = managerService.delSelectedUser(ids);
        modelAndView.setViewName("redirect:"+request.getContextPath()+"/managerController");
        return modelAndView;
    }

    @RequestMapping("/findUserById/{uid}")
    public ModelAndView findUserById(@PathVariable Integer uid){
        System.out.println("enter...");
        ModelAndView modelAndView=new ModelAndView();
        User user = managerService.findUserById(uid);
        modelAndView.addObject("user",user);
        modelAndView.setViewName("updateUser");
        return modelAndView;
    }
}
